<?php

class Third_Entity extends Third_DomAction {

	public $xml;
	public $base = "/entity";
	public $editing = false;
	public $fields_fk = false;
	public $xml_path;
	public $config;

	function __construct($file = null, $editing = false, $create_temp = false){

			$this->config = Zend_Registry::get('config');

			$this->xml_path = APPLICATION_PATH.'/xml/';

			$this->file_tmp = $this->xml_path.'tmp/'.$file;//.".xml";
			$file 			= $this->xml_path.$file;// .".xml";

			$this->entity_file = $file;

			if( $create_temp == true ){
				copy($file, $this->file_tmp);
				$file = $this->file_tmp;
			} else if($editing == true && file_exists($this->file_tmp)){
				$file = $this->file_tmp;
			}

			$this->file = $file;

			if(file_exists( $file ) )
				parent::__construct($file);

			$this->setConfigs();
			$this->setFields();
	}

	function setCampo($campo){
		$this->campo = $campo;
	}

	function setFields(){
		$this->setCaminho($this->base.'/fields');
		$this->fields = $this->getCampos();
	}

	function getFields( $mode = 0 ){
		if(is_array( $this->fields )){
			foreach ( $this->fields as $key=>$data){
				if( $mode == 0 )
					$this->fields[$key] = $this->getFieldOptions($key);
				elseif( $mode == 1 )
					$this->fields[$key] = $key;
			}
		}
		return $this->fields;
	}

	function setConfigs(){
		$this->setCaminho($this->base.'/configs');
		$this->configs = $this->getCampos();
	}

	function getConfigs(){
		return $this->configs;
	}

	function getField( $field ){
		$this->setCampo($field);

		$return['properties'] 	= $this->getFieldProperties();
		$return['listOptions'] 	= $this->getFieldListOptions();
		$return['actions'] 		= $this->getFieldActions();
		$return['validations'] 	= $this->getFieldValidations();

		// SETA CAMPOS QUE SAO CHAVE ESTRANGEIRA
		if( $return['properties']['fk_type'] == 'MUL'){
			$this->fields_fk[] = $field;
		}

		return $return;
	}

	function getFieldOptions($field){

		$data = $this->getField($field);

		foreach($data as $values){
			if(is_array($values)){
				$return['table'] = $this->configs["table"];
				foreach($values as $key=>$value){
						$return[$key] = $value;
				}
			}
		}
		return $return;
	}

	function getFieldProperties(){

		$this->setCaminho( $this->base.'/fields'."/$this->campo"."/properties" );
		$fields = $this->getCampos();

		return $fields;
	}

	function getFieldListOptions(){
		$this->setCaminho( $this->base.'/fields'."/$this->campo"."/listOptions" );
		$fields = $this->getCampos();

		return $fields;
	}

	function getFieldActions(){
		$this->setCaminho( $this->base.'/fields'."/$this->campo"."/actions" );
		$fields = $this->getCampos();

		return $fields;
	}

	function getFieldValidations(){
		$this->setCaminho( $this->base.'/fields'."/$this->campo"."/validations" );
		$validations = $this->getCampos();

		return  $validations;
	}

	function showCampos($xml, $values = NULL ){
		$properties = new Entity( $xml);

		$frmBase = new Form( 'Frm', '', $values );
		$frmBase->setEditing( true );
		$frmBase->generateXML( $properties , $values);
		$campos = $frmBase->display('full');

		foreach( $campos['fields_display'] as $campo)
			$return .= utf8_decode( $campo );

		return $return;
	}

	/*
	 * ALTERA OU ADICIONA NOVOS NODES
	 * */
	function XmlDataArrayUpdate($array, $nodes, $type = 'single_node' ){
		$fields = $this->getFields();

		if(is_array($fields)){

			// PREENCHE CAMPOS COM DADOS CONTIDOS NO ARRAY
			foreach( $fields as $keyFileds=>$field){
				$dataFields = $this->getFieldOptions($keyFileds);

				$data[$keyFileds] = $dataFields;
			}
		}

		switch( $type ){
			case 'single_node':
				// PREENCHE CAMPOS NAO POSTADOS
				$nom_posted_fields = array_diff_key( $data[$nodes] , $array );
				foreach($nom_posted_fields as $key=>$value){
					$array[$key] = '';
				}

				if($nodes)
					$data[$nodes] = $array;
			break;
			case 'multiple_node':
				foreach($array as $value){
					$fields_update[$value] = '';
				}

				// REMOVE CAMPOS
				/*
				$node_remove = array_diff_key( $data, $fields_update  );
				foreach( array_keys( $node_remove ) as $to_remove )
					unset($data[$to_remove]);
				*/

				// ADICIONA CAMPOS
				$node_add = array_diff_key( $fields_update, $data   );
				foreach( array_keys( $node_add ) as $to_add )
					$data[$to_add] = '';

			break;
			}

		return $data;
	}
}
?>